# ============================================================
# * Copyright © hk. 2022-2025. All rights reserved.
# * File name: CMakeLists.txt
# * Author   : 苏木
# * Date     : 2025-05-22
# * Version  : 
# * Description: cmake version 3.16.3
# ============================================================
## https://cmake.com.cn/cmake/help/latest/guide/tutorial/index.html
## https://cmake.org/cmake/help/latest/

# 指定 CMake 的最低版本要求
# --------------------------------------------------
cmake_minimum_required(VERSION 3.10)


# 设置项目名称和使用的编程语言
# --------------------------------------------------
project(SPROJECT CXX)
set(app_target "app_demo.out")

# 启用/禁用在生成期间输出编译命令。会生成compile_commands.json文件,包含编译命令，可以被clangd使用实现跳转和补全
# --------------------------------------------------
# 注意,这个选项放在PROJECT之前的话可能导致第一次构建不生效
# SET(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "Enable compile commands export" FORCE) # 强制设置变量并更新缓存
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)


# 设置C++编译器（一般不推荐写在这里，影响可移植性）
# --------------------------------------------------
# set(CMAKE_CXX_COMPILER "g++")          # 直接指定编译器名称（适用于已在PATH中的编译器）
set(CMAKE_CXX_COMPILER "/usr/bin/g++")  # 修改为实际的g++路径
message(STATUS "[USER:TOP]CMAKE_CXX_COMPILER : ${CMAKE_CXX_COMPILER}") # 显示当前使用的编译器路径


# 设置C++编译选项,要放在 add_executable 之前
# --------------------------------------------------
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
add_compile_options(-g -Wall) # 不会去修改 CMAKE_CXX_FLAGS，一般还是建议用这个


# 指定可执行程序和源文件
# --------------------------------------------------
set(src_list app_demo.cpp)                                  # 设置main函数源文件
add_executable(${app_target} ${src_list})


# 生成uninstall目标
# --------------------------------------------------
# 配置 uninstall 脚本
configure_file(
    "${CMAKE_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY
)

# 添加 uninstall 目标
add_custom_target(uninstall
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
    COMMENT "Uninstalling..."
)

# 获取目标的属性,要先指定目标才能获取
# --------------------------------------------------
get_target_property(app_compile_options ${app_target} COMPILE_OPTIONS) # 获取编译选项属性
message(STATUS "[USER:TOP]Target Compile Options: ${app_compile_options}")
message(STATUS "[USER:TOP]CMAKE_CXX_FLAGS       : ${CMAKE_CXX_FLAGS}")      # cxx编译选项

# install: cmake -DCMAKE_INSTALL_PREFIX=/usr
# --------------------------------------------------
# 1. 安装目标文件
install(TARGETS ${app_target}
    	RUNTIME DESTINATION bin
)

# 打印安装目录信息
message(STATUS "[USER:TOP]install path: ${CMAKE_INSTALL_PREFIX}")

# 最终安装目录的结构如下：
# .
# └── bin
#     └── app_demo.out
